package com.justdoit.addressbook.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Iterator;

import com.justdoit.addressbook.domain.Contact;
import com.justdoit.addressbook.service.ContactService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.Authentication;

@Controller
public class ContactController {

    @Autowired
    private ContactService contactService;

    @RequestMapping("/index")
    public String listContacts(Map<String, Object> map) {

    	Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName(); //get logged in username
   
        map.put("username", name);
    	
        map.put("contact", new Contact());
        map.put("contactList", contactService.listContact());

        return "contact";
    }
    
    @RequestMapping(value = "/list",  method = RequestMethod.POST)
    public @ResponseBody List<Contact> listContactsJSON() {

        return contactService.listContact();
    }
    
    @RequestMapping(value = "/addJSON", method = RequestMethod.POST )
    public @ResponseBody List<Contact> addContactJSON( @RequestParam String firstname, @RequestParam String lastname,
    		                                           @RequestParam String email, @RequestParam String telephone ) {


    	Contact contact=new Contact();
    	contact.setFirstname(firstname);
    	contact.setLastname(lastname);
    	contact.setEmail(email);
    	contact.setTelephone(telephone);
    	
        contactService.addContact(contact);

        return contactService.listContact();
    }
    @RequestMapping("/")
    public String home() {
        return "redirect:/index";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("contact") Contact contact,
            BindingResult result) {

     //   contactService.addContact(contact);

        return "redirect:/index";
    }

    @RequestMapping(value = "/delete/{contactId}", method = RequestMethod.POST)
    public @ResponseBody List<Contact> deleteContactJSON(@PathVariable("contactId") Integer contactId) {

      //  contactService.removeContact(contactId);

        return contactService.listContact();
    }
    
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody List<Contact> deleteContactsJSON(@RequestBody List<String> ArrPersons) {
 
    	
    	for(String contactId: ArrPersons){
   	    	contactService.removeContact(Integer.parseInt(contactId));
    	}
    //	System.out.println(ArrPersons.toString());
    	
//    	Iterator it = ArrPersons.iterator();
//    	while(it.hasNext()){
//    		contactService.removeContact((Integer)it.next());	
//    	}
    	
        return contactService.listContact();
    }
  /*  @RequestMapping("/delete/{contactId}")
    public String deleteContact(@PathVariable("contactId") Integer contactId) {

        contactService.removeContact(contactId);

        return "redirect:/index";
    }*/
}